package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.R;
import com.qire.manhua.dal.IComicDataDAL;
import com.qire.manhua.model.viewModel.impl.ComicRankListOfHotViewModel;
import com.qire.manhua.model.vo.ComicRankListOfHotVO;
import com.qire.manhua.model.vo.ComicRankListOfHotVO.ComicRankListItemVO;
import com.qire.manhua.view.attributeBinder.attribute.ImageAttribute;

public class ComicRankListOfHotService extends DataService {

    private final IComicDataDAL comicDataDAL = WarehouseFactory.create(this, IComicDataDAL.class);
    private final ComicRankListOfHotViewModel viewModel;

    private final String[] rankTypeArray = {"free", "sell", "new", "end"};
    private int tabIndex = 0;

    public ComicRankListOfHotService(ComicRankListOfHotViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 刷新 {@code tabIndex} 指向类型数据，并移动下一个直到遍历排行榜类型最后一个。
     */
    public void loadNextTabData() {
        if(tabIndex < rankTypeArray.length) {
            viewModel.setCurrentTabIndex(tabIndex);
            fetchComicRankList(rankTypeArray[tabIndex++]);
        }
    }

    /**
     * 获取排行榜数据根据排行榜类型
     * @param rankType 排行榜类型值域 {"free","sell","new","end"}
     */
    public void fetchComicRankList(String rankType) {
        comicDataDAL.fetchComicRankListOfHot(rankType);
    }
    @StrategyAnnotation(event = Url.comicRank)
    public void fetchComicRankListOfHotCallBack(ComicRankListOfHotVO dataVO, boolean isCache) {
        if(ObjectUtil.nonNull(dataVO)) {
            // 设置下次更新时间提示
            viewModel.setUpdateTimeTips(dataVO.notice);

            // 刷新排行榜列表数据，如果有数据。
            if(ArrayUtil.notEmpty(dataVO.list)) {
                int serialNumber = 1;
                for(ComicRankListItemVO vo : dataVO.list) {
                    int          comicId            = vo.id;
                    ImageAttribute comicCover       = new ImageAttribute(vo.book_unruly, R.drawable.app_placeholder220x290);
                    CharSequence floatLayerText     = String.format("更新至%s话", vo.chapter_px);
                    CharSequence comicName          = vo.book_name;
                    CharSequence comicAuthor        = String.format("作者: %s", vo.book_author);
                    CharSequence comicBrief         = vo.short_title;
                    CharSequence comicLabel         = vo.comicLabel();
                    CharSequence comicPopularity    = String.format("人气值: %s", StringUtil.toSymbol(vo.read_cnt));
                    int          comicSerialNumber  = serialNumber++;
                    viewModel.addRankItemViewModel(comicId, comicCover, floatLayerText, comicName, comicAuthor, comicBrief, comicLabel, comicPopularity, comicSerialNumber);
                }
                viewModel.refreshRankUI();
            }
        }
        // 加载下一个
        loadNextTabData();
    }

}
